﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ScreenMonkey.Plugin.Clips.Song
{
    public partial class SongTagsEditor : Form
    {
        public SongTagsEditor()
        {
            InitializeComponent();

            LoadTags();
        }

        private void LoadTags()
        {
            tagList.Items.Clear();
            foreach(VerseLabel tag in VerseLabels.Labels)
            {
                AddTagToList(tag);
            }
        }

        private void AddTagToList(VerseLabel tag)
        {
            ListViewItem newItem = new ListViewItem(tag.ToString());
            newItem.SubItems.Add(tag.QuickName);
            newItem.Tag = tag;

            tagList.Items.Add(newItem);
        }

        private void OKButton_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void CancelSelect_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void tagList_SelectedIndexChanged(object sender, EventArgs e)
        {
            RefreshSelection();
        }

        private void RefreshSelection()
        {
            if (tagList.SelectedItems.Count > 0)
            {
                VerseLabel tag = tagList.SelectedItems[0].Tag as VerseLabel;
                if (tag != null)
                {
                    tagProperties.SelectedObject = tag;
                }
            }
            else
                tagProperties.SelectedObject = null;
        }

        private void addTag_Click(object sender, EventArgs e)
        {
            int index = 1;
            VerseLabel newTag = new VerseLabel(string.Format("New Tag {0}", index), "");
            while (VerseLabels.FindLabel(newTag.Id) != null)
            {
                newTag.Name = string.Format("New Tag {0}", index);
                index++;
            }
            
            VerseLabels.Add(newTag);
            AddTagToList(newTag);
            tagProperties.SelectedObject = newTag;
        }

        private void deleteTag_Click(object sender, EventArgs e)
        {
            if (tagList.SelectedItems.Count > 0)
            {
                VerseLabel tag = tagList.SelectedItems[0].Tag as VerseLabel;
                if (tag != null)
                {
                    VerseLabels.Delete(tag);
                    LoadTags();
                    RefreshSelection();
                }
            }
        }

    }
}
